package com.junci.mtest.views.adapter;

import android.content.Context;
import android.graphics.Typeface;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.junci.mtest.R;
import com.junci.mtest.databinding.ItemEduBinding;
import com.junci.mtest.databinding.ItemPlanBinding;
import com.junci.mtest.model.bean.TestPlanBean;
import com.junci.mtest.utils.DisplayUtil;
import com.junci.mtest.vm.PlanViewModel;

import java.util.List;

/**
 * Created by YM
 * Create Date 2024/4/30 19:08
 * Used
 */
public class PlanActivityEduAdapter extends RecyclerView.Adapter<PlanActivityEduAdapter.ViewHolder>{

    private Context context ;
    PlanViewModel viewModel;

    private int selectPostion = 0 ;
    public PlanActivityEduAdapter(Context context, PlanViewModel viewModel) {
        this.context = context;
        this.viewModel = viewModel;
    }

    public int getSelectPostion() {
        return selectPostion;
    }

    public void setSelectPostion(int selectPostion) {
        int last = this.selectPostion;
        this.selectPostion = selectPostion;
        notifyItemChanged(last);
        notifyItemChanged(selectPostion);
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        ItemEduBinding binding = DataBindingUtil.inflate(LayoutInflater.from(parent.getContext()), R.layout.item_edu, parent, false);
        return new ViewHolder(binding);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        ItemEduBinding binding = holder.getBinding();
        TestPlanBean.Data.Rows.Edus bean = this.items.get(position);
        binding.setItem(bean);

        Glide.with(context).load(binding.getItem().getPic_url()) //图片地址
                .override((int) (DisplayUtil.getWidth()/1.5), (int) (DisplayUtil.getWidth()/1.5*0.8)). //设置最终显示的图片像素为80*80,注意:这个是像素,而不是控件的宽高
                centerCrop(). //中心裁剪,缩放填充至整个ImageView
//                skipMemoryCache(true). //跳过内存缓存
                diskCacheStrategy(DiskCacheStrategy.ALL). //保存最终图片
                thumbnail(1f). //设置缩略图
                into(binding.ivPlan);
        binding.tvPlanTitle.setText(bean.getName());
        if (position==selectPostion) {
            binding.tvPlanTitle.setGravity(Gravity.CENTER);
            binding.tvPlanTitle.setPadding(0,0,0,0);
            binding.tvPlanTitle.setTypeface(null, Typeface.BOLD);

        }else{
            binding.tvPlanTitle.setGravity(Gravity.LEFT|Gravity.BOTTOM);
            binding.tvPlanTitle.setPadding(DisplayUtil.dip2px(25),0,0,DisplayUtil.dip2px(25));
            binding.tvPlanTitle.setTypeface(null, Typeface.NORMAL);
        }
        binding.getRoot().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
            }
        });



    }


    @Override
    public int getItemCount() {
        if (items != null) {
            return items.size();
        }else {
            return 0;
        }
    }
    private List<TestPlanBean.Data.Rows.Edus> items;
    public void setItems(List<TestPlanBean.Data.Rows.Edus> items) {
        this.items = items;
        notifyDataSetChanged();
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        private ItemEduBinding binding;
        public ViewHolder(@NonNull ItemEduBinding binding) {
            super(binding.getRoot());
            this.binding = binding;
        }

        public ItemEduBinding getBinding() {
            return binding;
        }

    }

}
